package com.derrick.mybatisplusgenerate;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;


public class Generate {
    //输出路径
    private static final String gcOutPutDir = System.getProperty("user.dir") + "/src/main/java";
    //作者
    private static final String gcAuthor = "Derrick";

    //数据源
    private static final String dscUrl = "jdbc:mysql://localhost:3306/sys_my_test?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8";
    private static final String dscDriver = "com.mysql.cj.jdbc.Driver";
    private static final String dscUserName = "root";
    private static final String dscPassWord = "123456";

    //在哪个包下
    private static final String pcParentName = "com.derrick.mybatisplus";
    //生成模块名称
    private static final String pcModuleName = "student";

    //对哪个表生成
    private static final String tableName = "student";



    public static void main(String[] args) {
        AutoGenerator autoGenerator = new AutoGenerator();
        autoGenerator.setTemplateEngine(new FreemarkerTemplateEngine());    //配置模板
        autoGenerator.setGlobalConfig(getGlobalConfig());                   //配置全局
        autoGenerator.setDataSource(getDataSourceConfig());                 //配置数据源
        autoGenerator.setPackageInfo(getPackageConfig());                   //配置包
        autoGenerator.setStrategy(getStrategyConfig());                     //配置策略
        autoGenerator.execute();
    }

    public static GlobalConfig getGlobalConfig() {
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(gcOutPutDir);
        gc.setAuthor(gcAuthor);
        gc.setOpen(false);  //生成后是否打开资源管理器
        gc.setFileOverride(false);  //重新生成时,文件是否覆盖
        gc.setServiceName("%sService"); //去掉Service接口首字母I
        gc.setIdType(IdType.ID_WORKER_STR);   //主键策略
        gc.setDateType(DateType.ONLY_DATE);   //定义生成的实体中日期类型
        gc.setSwagger2(false);  //开启Swagger模式

        return gc;
    }

    public static DataSourceConfig getDataSourceConfig() {
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(dscUrl);
        dsc.setDriverName(dscDriver);
        dsc.setUsername(dscUserName);
        dsc.setPassword(dscPassWord);
        dsc.setDbType(DbType.MYSQL);

        return dsc;
    }

    public static PackageConfig getPackageConfig() {
        PackageConfig pc = new PackageConfig();
        pc.setModuleName(null);     //模块名
        pc.setParent(pcParentName);
        pc.setModuleName(pcModuleName);
        pc.setController("controller");
        pc.setEntity("entity");
        pc.setService("service");
        pc.setMapper("mapper");

        return pc;
    }

    public static StrategyConfig getStrategyConfig() {
        StrategyConfig sc = new StrategyConfig();
        sc.setInclude(tableName);  //对哪一张表生成代码
        sc.setNaming(NamingStrategy.underline_to_camel);    //数据库映射表到实体命名用下划线转驼峰
        sc.setTablePrefix(getPackageConfig().getModuleName() + "_");

        sc.setColumnNaming(NamingStrategy.underline_to_camel);
        sc.setEntityLombokModel(true);  //开启lombok

        sc.setRestControllerStyle(true);    //restful api风格
        sc.setControllerMappingHyphenStyle(true);   //url中驼峰转述字符

        return sc;
    }
}
